package com.example.recordingpen_datacollection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.recordingpen_datacollection.Utils.String.StringUtils;
import com.example.recordingpen_datacollection.entity.*;
import com.example.recordingpen_datacollection.entity.Query.OfficialNoticeQue;
import com.example.recordingpen_datacollection.entity.Vo.OfficialNoticeVo;
import com.example.recordingpen_datacollection.mapper.OfficialNoticeMapper;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.service.IdentificationService;
import com.example.recordingpen_datacollection.service.LoginService;
import com.example.recordingpen_datacollection.service.OfficialNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xg
 * @since 2024-06-28
 */
@Service
public class OfficialNoticeServiceImpl extends ServiceImpl<OfficialNoticeMapper, OfficialNotice> implements OfficialNoticeService {

    @Autowired
    private LoginService loginService;
    @Autowired
    private IdentificationService identificationService;

    @Override
    public boolean setOfficialNotice(OfficialNoticeVo officialNoticeVo, Integer value) {
        OfficialNotice officialNotice = new OfficialNotice();
        officialNotice.setAid(value);
        officialNotice.setTitle(officialNoticeVo.getTitle());
        officialNotice.setMessage(officialNoticeVo.getMessage());
        boolean save = this.save(officialNotice);
        return save;
    }

    @Override
    public boolean alterOfficialNotice(OfficialNoticeVo officialNoticeVo, Integer value) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("aid",value);
        wrapper.eq("pid",officialNoticeVo.getPid());

        if(StringUtils.isNotEmpty(officialNoticeVo.getMessage()) || StringUtils.isNotEmpty(officialNoticeVo.getTitle())){

            OfficialNotice officialNotice = new OfficialNotice();
            if(StringUtils.isNotEmpty(officialNoticeVo.getTitle())){
                officialNotice.setTitle(officialNoticeVo.getTitle());
            }

            if(StringUtils.isNotEmpty(officialNoticeVo.getMessage())){
                officialNotice.setMessage(officialNoticeVo.getMessage());
            }


            boolean update = this.update(officialNotice, wrapper);
            return update;

        }
        return false;

    }

    @Override
    public RLIST getOfficialNoticePage(OfficialNoticeQue officialNoticeQue, Integer value) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("aid",value);
        //校验
        wrapper.orderByDesc("pid");
        Page<OfficialNotice> page = new Page<>(officialNoticeQue.getPageNo(), officialNoticeQue.getPageSize());
        this.page(page, wrapper);
        long total = page.getTotal();
        List<OfficialNotice> list = page.getRecords();

        long numPage = 0;
        if(total == 0){
            numPage = 1L;
        } else if(list.size() == 0){
            numPage = 1L;
        } else {
            // 每页条数
            numPage = (long) Math.ceil(total / (double)officialNoticeQue.getPageSize()) ;
        }
        if(list.size() == 0){
            return RLIST.error().message("暂无数据") ;
        }
        return RLIST.ok().data("data",list).data("total",total).data("numPage",numPage);
    }

    @Override
    public boolean handleOfficialNotice(Integer uid, Integer value) {
        Identification identification = new Identification();
        identification.setId(uid);
        identification.setTid(value);
        boolean update = identificationService.updateById(identification);

        return update;
    }
}
